/*
 * Copyright (C) 2011-2018 ShenZhen iBOXSaaS Information Technology Co.,Ltd.
 * 
 * All right reserved.
 * 
 * This software is the confidential and proprietary information of iBOXSaaS Company of China.
 * ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the contract agreement you entered into with iBOXSaaS inc.
 * 
 */

package com.iboxpay.open.auth.center.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.iboxpay.open.auth.center.enums.MethodEnums;
import com.iboxpay.open.auth.center.validate.BaseParamValidate;
import com.iboxpay.open.auth.center.validate.ParamValidate;
import com.iboxpay.open.auth.center.validate.impl.AppIdValidate;
import com.iboxpay.open.auth.center.validate.impl.AppSecretValidate;
import com.iboxpay.open.auth.center.validate.impl.AuthCodeValidate;
import com.iboxpay.open.auth.center.validate.impl.ClientCredentialsGrantValidate;
import com.iboxpay.open.auth.center.validate.impl.AuthorizationCodeGrantValidate;
import com.iboxpay.open.auth.center.validate.impl.RefreshTokenGrantValidate;
import com.iboxpay.open.auth.center.validate.impl.RefreshTokenValidate;
import com.iboxpay.open.auth.center.validate.impl.ResponseTypeValidate;
import com.iboxpay.open.auth.center.validate.impl.ScopeValidate;

/**
 * 参数验证配置类 ParamValidateConfig.java
 * 
 * @author xuxiao
 * @date 2018/06/19
 */
@Configuration
public class ParamValidateConfig {
    @Autowired
    private AppIdValidate appIdValidate;

    @Autowired
    private AuthCodeValidate authCodeValidate;

    @Autowired
    private AuthorizationCodeGrantValidate grantTypeValidate;

    @Autowired
    private ResponseTypeValidate responseTypeValidate;

    @Autowired
    private ScopeValidate scopeValidate;

    @Autowired
    private AppSecretValidate appSecretValidate;
    
    @Autowired
    private RefreshTokenValidate refreshTokenValidate;
    
    @Autowired
    private RefreshTokenGrantValidate refreshTokenGrantValidate;
    
    @Autowired
    private ClientCredentialsGrantValidate clientCredentialsGrantValidate;

    @Bean
    public List<ParamValidate> authUserConfirmValidateList() {
        List<ParamValidate> result = new ArrayList<>();
        result.add(appIdValidate);
        result.add(responseTypeValidate);
        result.add(scopeValidate);
        return result;
    }

    @Bean
    public List<ParamValidate> authUserAccessTokenValidateList() {
        List<ParamValidate> result = new ArrayList<>();
        result.add(appIdValidate);
        result.add(appSecretValidate);
        result.add(authCodeValidate);
        result.add(grantTypeValidate);
        return result;
    }
    
    @Bean
    public List<ParamValidate> authAppAccessTokenValidateList() {
        List<ParamValidate> result = new ArrayList<>();
        result.add(appIdValidate);
        result.add(scopeValidate);
        result.add(appSecretValidate);
        result.add(clientCredentialsGrantValidate);
        return result;
    }
    
    @Bean
    public List<ParamValidate> authRefreshTokenValidateList() {
        List<ParamValidate> result = new ArrayList<>();
        result.add(appIdValidate);
        result.add(appSecretValidate);
        result.add(refreshTokenValidate);
        result.add(refreshTokenGrantValidate);
        return result;
    }

    @Bean(name = "validates")
    public Map<String, List<ParamValidate>> validates(
        @Autowired @Qualifier("authUserConfirmValidateList") List<ParamValidate> authUserConfirmValidateList,
        @Autowired @Qualifier("authUserAccessTokenValidateList") List<ParamValidate> authUserAccessTokenValidateList, 
        @Autowired @Qualifier("authAppAccessTokenValidateList") List<ParamValidate> authAppAccessTokenValidateList,
        @Autowired @Qualifier("authRefreshTokenValidateList") List<ParamValidate> authRefreshTokenValidateList) {
        Map<String, List<ParamValidate>> result = new HashMap<>();
        result.put(MethodEnums.authUserConfirm.getCode(), authUserConfirmValidateList);
        result.put(MethodEnums.authUserAccessToken.getCode(), authUserAccessTokenValidateList);
        result.put(MethodEnums.authAppAccessToken.getCode(), authAppAccessTokenValidateList);
        result.put(MethodEnums.authRefreshToken.getCode(), authRefreshTokenValidateList);
        return result;
    }

    @Bean(name = "baseParamValidate")
    public BaseParamValidate
        baseParamValidate(@Autowired @Qualifier("validates") Map<String, List<ParamValidate>> validates) {
        return new BaseParamValidate(validates);
    }
}
